module game {
    /**
     * 玩家注册页面
    */
    export class PlayerForgetMediator extends BaseMediator {
        public static NAME: string = "PlayerForgetMediator";

        public constructor(viewComponent: any = null) {
            super(PlayerForgetMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.LOGIN.OPEN_PLAYFORGET,        //打开玩家注册页
                PanelNotify.LOGIN.CLOSE_PLAYFORGET,       //关闭玩家注册页
            ];
        }
        private PlayerForgetPanel: PlayerForgetPanel;
        private ViewStackIndex = 0;
        public handleNotification(notification: puremvc.INotification): void {
            var data: any = notification.getBody();
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.PlayerForgetPanel) {
                        this.PlayerForgetPanel.width = GameConfig.curWidth()
                        this.PlayerForgetPanel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.LOGIN.OPEN_PLAYFORGET:
                    this.PlayerForgetPanel = null;
                    this.PlayerForgetPanel = new PlayerForgetPanel();
                    this.showUI(this.PlayerForgetPanel, 2, 1, true);
                    break;
                case PanelNotify.LOGIN.CLOSE_PLAYFORGET:
                    this.closePanel(1);
                    break;
            }
        }

        public initUI(): void {
            this.PlayerForgetPanel.btn_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCloseBtnClick, this);
            this.PlayerForgetPanel.btn_ensure.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onEnsureBtnClick, this);
            this.PlayerForgetPanel.btn_getIdentifycode.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGetIdentufyCodeBtnClick, this);

            this.PlayerForgetPanel.eText_Code.inputType = egret.TextFieldInputType.PASSWORD;
            this.PlayerForgetPanel.eText_Code.displayAsPassword = true;
            this.PlayerForgetPanel.eText_Code2.inputType = egret.TextFieldInputType.PASSWORD;
            this.PlayerForgetPanel.eText_Code2.displayAsPassword = true;
        }

        //关闭注册页点击事件
        private onCloseBtnClick(event: egret.TouchEvent): void {
            Global.removeInterval(InstructionsEnum.GAME_INTERVAL_LIST)
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            this.closePanel(1);
        }

        //确认注册点击事件
        private onEnsureBtnClick(event: egret.TouchEvent): void {
            var _thisObj = this;
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            let phone = this.PlayerForgetPanel.eText_phoneNum.text;
            let Identifycode = this.PlayerForgetPanel.eText_Identifycode.text
            let pass = this.PlayerForgetPanel.eText_Code.text;
            let pass2 = this.PlayerForgetPanel.eText_Code2.text;
            if (Global.isEmpty(phone)) {
                Global.tipShow("请输入手机号!")
                return;
            }
            if (Global.isEmpty(Identifycode)) {
                Global.tipShow("请输入验证码!")
                return;
            }
            if (Global.isEmpty(pass)) {
                Global.tipShow("请输入密码!")
                return;
            }
            if (pass != pass2) {
                Global.tipShow("两次密码输入不一致!")
                return;
            }
            EffectUtils.showWaritPanel();
            GameVO.getVo().szAccounts = phone;
            GameVO.getVo().szPassword = pass;
            let sendobj = {};
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/H5Interface/ModifyUserPasspord?PhoneNum=" + phone + "&NewPassword=" + new utils.MD5().hex_md5(pass).toUpperCase()
                + "&PhoneCode=" + Identifycode + "&AuthToken=" + new utils.MD5().hex_md5(phone + "NH11MqbKJ5hVr9do").toUpperCase() + "&PlatFormName=yuezhong", egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send();
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    EffectUtils.hideWaritPanel();
                    if (obj.code == 0) {
                        _thisObj.closePanel(1);
                        Global.tipShow("密码修改成功！")
                    }
                    else {
                        Global.tipShow(obj.mess)
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
            // SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_HALL, RegDataGame.CMD_MB_RegAccountsSpreader, obj, true)
            // GameVO.getVo().registeAcount = true;
        }

        //获取短信验证码点击事件
        private onGetIdentufyCodeBtnClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            if (this.PlayerForgetPanel.eText_phoneNum.text.length == 11) {
                this.PlayerForgetPanel.btn_getIdentifycode.enabled = false;
                let time = 120;
                this.PlayerForgetPanel.identify_rem.text = "重新获取" + time
                this.PlayerForgetPanel.identify_rem.visible = true;
                this.PlayerForgetPanel.identify_rem;
                let t = egret.setInterval(() => {
                    time--
                    this.PlayerForgetPanel.identify_rem.text = "重新获取" + time
                    if (time == 0) {
                        this.PlayerForgetPanel.btn_getIdentifycode.enabled = true;
                        this.PlayerForgetPanel.identify_rem.visible = false;
                        egret.clearInterval(t)
                    }

                }, this, 1000)
                InstructionsEnum.GAME_INTERVAL_LIST.push(t);
                this.PlayerForgetPanel.identify_rem
                var request = new egret.HttpRequest();
                var paramas = {
                    "OptionType": 0,
                    "CountryCode": "86",
                    "PhoneNum": this.PlayerForgetPanel.eText_phoneNum.text,
                    "Mac": "221bd4b23889e4701f7db1f4a86dccb"
                };
                request.responseType = egret.HttpResponseType.TEXT;
                request.open("http://59.188.233.197:1000/GameWebApplication/GetPhoneCode.ashx", egret.HttpMethod.POST);
                request.setRequestHeader("Content-Type", 'text/plain;charset=UTF-8');
                request.send(JSON.stringify(paramas));
                request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                    try {
                        var request = <egret.HttpRequest>event.currentTarget;
                        var obj = JSON.parse(request.response)
                    } catch (err) {
                        EffectUtils.textFadeAway("数据包异常，无法解析!")
                    }
                }, this);
            }
            else {
                Global.tipShow("请输入正确的手机号");
            }
        }
    }
}